package cn.edu.siso.imhere;

import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.Timer;
import java.util.TimerTask;

public class HereActivity extends BaseActivity {

    private Timer m_Timer = null; // 颜色闪烁的定时器
    private Handler m_Handler = null; // 接收定时器消息的对象
    private int m_ColorIndex = -1; // 显示颜色的索引
    private TypedArray m_ColorArray = null; // 获取颜色ID数组对象
    private boolean isBlack = false; // 当前是否为黑色
    private boolean isTimerStop = true; // 定时器的暂定的标记

    private ImageView m_HereImage = null; // 显示颜色的主键

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_here);

        // 顶部工具栏
        RelativeLayout topBar = (RelativeLayout) findViewById(R.id.top_bar);
        topBar.getBackground().setAlpha(100); // 设置TOP Bar透明度
        // 设置顶部工具栏的图标
        ImageView leftIcon = (ImageView) topBar.findViewById(R.id.top_bar_left_icon);
        leftIcon.setImageResource(R.drawable.here_left_img_bg);
        // 设置顶部工具栏的点击事件
        leftIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        ImageView rightIcon = (ImageView) topBar.findViewById(R.id.top_bar_right_icon);
        rightIcon.setImageResource(R.drawable.here_right_img_bg);
        rightIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isTimerStop) {
                    resumeTimer();
                } else {
                    stopTimer();
                    // 把颜色作为背景设置到ImageView中，如果索引不在范围内这返回默认值0
                    m_HereImage.setBackgroundResource(m_ColorArray.getResourceId(m_ColorIndex, 0));
                }
            }
        });

        // 获取传递来的数据
        Bundle bundle = getIntent().getExtras();
        // 获取颜色索引
        m_ColorIndex = bundle.getInt(getResources().getString(R.string.color_bundle_key));
        // 获取颜色ID数组对象
        m_ColorArray = getResources().obtainTypedArray(R.array.here_color_array);
        // 显示的颜色
        m_HereImage = (ImageView) findViewById(R.id.here_color);
        // 把颜色作为背景设置到ImageView中，如果索引不在范围内这返回默认值0
        m_HereImage.setBackgroundResource(m_ColorArray.getResourceId(m_ColorIndex, 0));

        // 创建定时器消息处理对象
        m_Handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    if (isBlack) {
                        // 把颜色作为背景设置到ImageView中，如果索引不在范围内这返回默认值0
                        m_HereImage.setBackgroundResource(m_ColorArray.getResourceId(m_ColorIndex, 0));
                        isBlack = false;
                    } else {
                        // 把颜色作为背景设置到ImageView中，如果索引不在范围内这返回默认值0
                        m_HereImage.setBackgroundResource(android.R.color.black);
                        isBlack = true;
                    }
                }
            }
        };

        // 创建定时器
        m_Timer = new Timer();
        m_Timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (!isTimerStop) {
                    Message msg = new Message();
                    msg.what = 1;
                    m_Handler.sendMessage(msg);
                }
            }
        }, 0, 250);
    }

    /**
     * 恢复定时器
     */
    private void resumeTimer() {
        isTimerStop = false;
    }

    // 停止定时器
    private void stopTimer() {
        isTimerStop = true;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_here, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopTimer(); // 停止定时器
        m_Timer.cancel(); // 退出定时器
        m_ColorArray.recycle(); // 释放资源
    }
}
